package com.itheima.server.controller;

import com.itheima.model.domain.UserInfo;
import com.itheima.model.vo.PageResult;
import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    /*填写资料
    * UserInfo
    * 请求头中携带token
    * */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        //2.向userinfo中设置id
        userInfo.setId(UserHolder.getUserId());
        //3.调用service
        userInfoService.save(userInfo);
        //4.返回响应值
        return ResponseEntity.ok(null);
    }
    /*选取头像
    *和保存用户信息一样但是是修改信息（上传头像）
    *
    * */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {
        //2.向userinfo中设置id
        Long id = UserHolder.getUserId();
        //3.调用service
        userInfoService.updateHead(headPhoto,id);
        //4.返回响应值
        return ResponseEntity.ok(null);
    }

}
